# CMakeLists.txt for DWG plugin
# This plugin provides direct conversion from DWG files to OSG objects using libredwg
#include(ExternalProject)

message("Checking for libredwg")
# Check for libredwg
FIND_PATH(
        LIBREDWG_INCLUDE_DIR
        NAMES dwg.h
        HINTS ${CMAKE_SOURCE_DIR}/../../libs/libredwg/${ANDROID_ABI}/include
        NO_CMAKE_FIND_ROOT_PATH
)
#set(ENV{AND_OSG_LIB_NAMES} "$ENV{AND_OSG_LIB_NAMES} libjpeg")
#set(ENV{AND_OSG_LIB_PATHS} "$ENV{AND_OSG_LIB_PATHS}include ${JPEG_INCLUDE_DIR}/Android.mk \n")
if(LIBREDWG_INCLUDE_DIR)
    message(STATUS "libredwg found3 ${LIBREDWG_INCLUDE_DIR}" )
    set(REDWG_FOUND "Yes")
    FIND_LIBRARY(LIBREDWG_LIBRARY
            NAMES libredwg.a
            HINTS ${CMAKE_SOURCE_DIR}/../../libs/libredwg/${ANDROID_ABI}/lib
            NO_CMAKE_FIND_ROOT_PATH
    )
else(LIBREDWG_INCLUDE_DIR)
    message(STATUS "libredwg missing" )
endif()

# If libredwg is not found as a library, try to build it from source
IF(NOT LIBREDWG_LIBRARY)
    MESSAGE(STATUS "libredwg library not found, attempting to build from source")
    #从githhub上下载 https://github.com/Dylan-H/libredwg.git
#    IF(NOT EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/libredwg/CMakeLists.txt)
#        include(FetchContent)
#
#        # 声明要下载的项目信息
#        FetchContent_Declare(
#                libredwg # 自定义依赖项名称
#                GIT_REPOSITORY https://github.com/Dylan-H/libredwg.git # Git仓库地址
#                GIT_TAG        master  # 可以是分支名（如main）、标签（如v1.0）或提交哈希
#                SOURCE_DIR     ${CMAKE_CURRENT_SOURCE_DIR}/libredwg # 关键：指定下载到当前CMakeLists.txt所在目录下的external_lib文件夹
#                CMAKE_ARGS     -DBUILD_SHARED_LIBS=OFF
#        )
#
#        # 执行下载并使其可用
#        FetchContent_MakeAvailable(libredwg)
#    ENDIF()
    # Check if we have the libredwg source
#    IF(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/libredwg/CMakeLists.txt)
#        # Build libredwg as a static library
#        ADD_SUBDIRECTORY(libredwg)
#        SET(LIBREDWG_LIBRARY libredwg)
#        SET(LIBREDWG_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/libredwg/include
#            ${CMAKE_CURRENT_SOURCE_DIR}/libredwg/src
#            ${CMAKE_CURRENT_SOURCE_DIR}/libredwg/armv8a/src)
#        message("dwg>>>>>>>>>>${CMAKE_CURRENT_SOURCE_DIR}")
#        link_directories(${CMAKE_CURRENT_SOURCE_DIR}/libredwg/build/Debug)
#    ELSE()
#        MESSAGE(FATAL_ERROR "libredwg source not found. Please ensure libredwg is available.")
#    ENDIF()
ENDIF()

# Check if we found libredwg
IF(LIBREDWG_INCLUDE_DIR AND LIBREDWG_LIBRARY)

    MESSAGE(STATUS "Found libredwg: ${LIBREDWG_LIBRARY}")
    MESSAGE(STATUS "libredwg include dir: ${LIBREDWG_INCLUDE_DIR}")
    
    INCLUDE_DIRECTORIES(${LIBREDWG_INCLUDE_DIR})
    SET(TARGET_LIBRARIES ${TARGET_LIBRARIES} ${LIBREDWG_LIBRARY})

    # Add compile definitions for libredwg
    ADD_DEFINITIONS(-DHAVE_LIBREDWG -DENABLE_SHARED)
#
#    # Create the plugin
#    ADD_LIBRARY(${TARGET_DEFAULT_PREFIX}dwg SHARED ${TARGET_SRC} ${TARGET_HDR})
#    TARGET_LINK_LIBRARIES(${TARGET_DEFAULT_PREFIX}dwg ${TARGET_LIBRARIES})
#
#    # Set target properties
#    SET_TARGET_PROPERTIES(${TARGET_DEFAULT_PREFIX}dwg PROPERTIES
#        FOLDER "Plugins"
#        LABEL "${TARGET_DEFAULT_LABEL_PREFIX} DWG"
#    )
#
#    # Install the plugin
#    INSTALL(TARGETS ${TARGET_DEFAULT_PREFIX}dwg
#        RUNTIME DESTINATION ${OSG_PLUGINS}
#        LIBRARY DESTINATION ${OSG_PLUGINS}
#        ARCHIVE DESTINATION ${OSG_PLUGINS}
#    )

    SET(TARGET_SRC
            #ReaderWriterDWG.cpp
            #SimpleDWGReader.cpp
            scene.cpp
            DWGReader.cpp
    )

    SET(TARGET_H
            scene.h
            DWGReader.h
    )


    SET(TARGET_ADDED_LIBRARIES osgText )

    #### end var setup  ###
    SETUP_PLUGIN(dwg)
    
ELSE()
    MESSAGE(WARNING "libredwg not found. DWG plugin will not be built.")
    MESSAGE(STATUS "To build DWG plugin, please install libredwg or ensure the source is available.")
ENDIF()
